// components/date-picker/date-picker.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    currDate: {
      type: String,
      value: ''
    },
    startTime:{
      type:String,
      value:''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    currYear: '',
    currMonth: '',
    currDayArr: [],
    currDay: '',
    startYear: '',
    startMonth: '',
    startDay:'',
    nowMonth: '',
    nowYear: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeDay(e) {
      let currDay = e.target.dataset.day;
      this.setData({
        currDay: currDay
      })
      let _month = (this.data.currMonth + '').length == 1 ? '0' + this.data.currMonth : this.data.currMonth
      let _day = (this.data.currDay + '').length == 1 ? '0' + this.data.currDay : this.data.currDay
      let date = this.data.currYear + '-' + _month+'-' + _day
      this.triggerEvent("getDate", date)
    },
    changeMonth(e) {
      if (e.target.dataset.state == 'desc') {
        if (this.data.currMonth == 1) {
          this.setData({
            currMonth: 12,
            currYear: this.data.currYear - 1
          })
        } else {
          this.setData({
            currMonth: this.data.currMonth - 1
          })
        }
        var _currDayArr = [];
        if(this.data.startYear == this.data.currYear&&this.data.currMonth == this.data.startMonth){
          let date = new Date()
          date.setMonth(this.data.currMonth)
          date.setDate(0)
          for (let i = this.data.startDay; i <= date.getDate(); i++) {
            _currDayArr.push(i)
          }
        }else{
          let date = new Date()
          date.setMonth(this.data.currMonth)
          date.setDate(0)
          for (let i = 1; i <= date.getDate(); i++) {
            _currDayArr.push(i)
          }
        }
        
        this.setData({
          currDayArr: _currDayArr
        })
      } else {
        if (this.data.currMonth == 12) {
          this.setData({
            currMonth: 1,
            currYear: this.data.currYear + 1
          })
        } else {
          this.setData({
            currMonth: this.data.currMonth + 1
          })
        }
        let date = new Date()
        if(this.data.currYear == date.getFullYear()&&this.data.currMonth == date.getMonth()+1){
          var _currDayArr = [];
          for (let i = 1; i < date.getDate(); i++) {
            _currDayArr.push(i)
          }
        }else{
          date.setMonth(this.data.currMonth)
          date.setDate(0)
          var _currDayArr = [];
          for (let i = 1; i <= date.getDate(); i++) {
            _currDayArr.push(i)
          }
        }
       
        this.setData({
          currDayArr: _currDayArr
        })
      }
    }
  },
  ready() {
    let date = new Date()
    let currYear = date.getFullYear();
    let currMonth = date.getMonth() + 1;
    this.setData({
      currYear: Number(this.data.currDate.substring(0, 4)),
      currMonth: Number(this.data.currDate.substring(5, 7)),
      currDay: Number(this.data.currDate.substring(8, 10)),
      startYear: Number(this.data.startTime.substring(0, 4)),
      startMonth: Number(this.data.startTime.substring(5, 7)),
      startDay: Number(this.data.startTime.substring(8, 10)),
      nowMonth: currMonth,
      nowYear: currYear,
    })
    let _day = date.getDate();
    var _currDayArr = [];
   
    if(this.data.currMonth == this.data.nowMonth&&this.data.nowYear==this.data.currYear&&this.data.startYear == this.data.currYear&&this.data.currMonth == this.data.startMonth){
      let _day = date.getDate();
      var _currDayArr = [];
      for (let i = this.data.startDay; i < _day; i++) {
        _currDayArr.push(i)
      }
    }else if(this.data.currMonth == this.data.nowMonth&&this.data.nowYear==this.data.currYear){
      let _day = date.getDate();
      var _currDayArr = [];
      for (let i = 1; i < _day; i++) {
        _currDayArr.push(i)
      }
    }else if(this.data.startYear == this.data.currYear&&this.data.currMonth == this.data.startMonth){
      for (let i = this.data.startDay; i < _day; i++) {
        _currDayArr.push(i)
      }
    }else{
      date.setMonth(this.data.currMonth)
      date.setDate(0)
      var _currDayArr = [];
      for (let i = 1; i <= date.getDate(); i++) {
        _currDayArr.push(i)
      }
    }
    this.setData({
      currDayArr: _currDayArr,
     
    })
  }
})